<?php
/**
 *	KLF Framework
 *
 *	Written by Kristian Oye <kristianoye@gmail.com> on Oct 25, 2011
 *	Copyright (C) 2010-2011
 */
namespace KLF\System\Cache
{
	use KLF\System\KComponent;

	/**
	 *  Class KCache 
	 *
	 *  The base cache class for all caching implementations 
	 *  used by the framework.
	 *
	 *  @package KLF\System\Cache
	 *  @access public
	 *  @author Kristian Oye <kristianoye@gmail.com>
	 *  @since 0.1
	 */
	abstract class KCache extends KComponent implements ICache, \ArrayAccess
	{
		/**
		* Returns true if the item exists in the cache.
		*
		* @param string $offset
		*/
		public function offsetExists($offset)
		{
			if (!is_string($offset))
			{
				throw new KArgumentException("Cache index must be a string");
			}
			return ($this->GetValue($offset) !== FALSE);
		}
		
		/**
		 * Array access shortcut to the cache.
		 * 
		 * @param string $offset
		 */
		public function offsetGet($offset)
		{
			if (!is_string($offset))
			{
				throw new KArgumentException("Cache index must be a string");
			} 
			return $this->GetValue($offset);
		}
		
		/**
		 * Set the value in the cache.
		 * 
		 * @param string $offset
		 * @param mixed $value
		 * @throws KArgumentException
		 */
		public function offsetSet($offset, $value)
		{
			if (!is_string($offset))
			{
				throw new KArgumentException("Cache index must be a string");
			}
			$this->SetValue($offset, $value);
		}
		
		/**
		 * Deletes a value from the cache.
		 * 
		 * @param string $offset
		 * @throws KArgumentException
		 */
		public function offsetUnset($offset)
		{
			if (!is_string($offset))
			{
				throw new KArgumentException("Cache index must be a string");
			}
			$this->DeleteValues($offset);
		}
	}
}
